import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

@WebServlet(urlPatterns = {"/signup"})
public class signup extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
	String URL=(String)getServletContext().getInitParameter("db.url");
	String NAME=(String)getServletContext().getInitParameter("db.user");
	String PASSWORD=(String)getServletContext().getInitParameter("db.password");
	try (Connection conn = DriverManager.getConnection(URL, NAME, PASSWORD)) {
	    Statement stat = conn.createStatement();

	    stat.execute(" insert into `user` (`username`,`passwd`,`familyname`,`name`,`email`) values ('"+req.getParameter("username")+"','"+req.getParameter("passwd")+"', '"+req.getParameter("name").substring(0,1)+"','"+req.getParameter("name").substring(1,req.getParameter("name").length())+"','"+req.getParameter("email")+"' );");

	    try{
		req.getRequestDispatcher("index.html").forward(req,res);
	    }catch (ServletException e){
		e.printStackTrace();
	    }
	    
	}catch (SQLException e){
	    e.printStackTrace();
	}
    }
}
